/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.iwords.teacher.web;

import com.iwords.ejb.TeacherEntityFacade;
import com.iwords.service.object.TeacherEntity;
import com.iwords.service.persistence.TeacherPersistence;
import java.io.IOException;
import java.util.Date;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author yunzi
 */
@WebServlet(name = "TeacherServlet", urlPatterns = {"/teacher"})
public class TeacherServlet extends BaseServlet<TeacherEntity> {
    
    public void init(){
        
    }

    /**如果command不能match到任何方法,默认显示的页面*/
    @Override
    protected void defaultAction(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        forward(request, response, "/teacher/login.jsp");
    }

    @EJB
    TeacherEntityFacade teacherFacade;
    void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("UserServlet.register() idParam = "+request.getParameter("id"));
        String idParam = request.getParameter("idParam");
        TeacherEntity teacher = null;
        if (idParam != null && idParam.length() > 0) {
            teacher = teacherFacade.find(Integer.valueOf(idParam));
        } else {
            teacher = new TeacherEntity();
        }
        request.setAttribute("teacher", teacher);
        forward(request, response, "/teacher/teacherInfo.jsp");
    }

    void save(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String idParam = request.getParameter("id");
        try {
            System.out.println("asdfasdfdf");
            String password = request.getParameter("password");
            String confirm = request.getParameter("confirm");
            String teachername = request.getParameter("teachername");
            if (teachername == null || teachername.length() < 5) {
                throw new Exception("jiaoshjiaoshi1jiaoshi名不能为空，且长度至少为5.");
            }
            if (password == null || password.length() < 1 || confirm == null || confirm.length() < 1) {
                throw new Exception("密码不能为空。");
            }
            if (!password.equals(confirm)) {
                throw new Exception("两次密码输入不一致.");
            }
            if (idParam == null || idParam.length() == 0) {
                TeacherEntity teacher = new TeacherEntity();
                teacher.setTeachername(teachername);
                teacher.setPassword(password);
                teacher.setCreatetime(new Date(System.currentTimeMillis()));
                teacherFacade.create(teacher);
                request.getSession(true).setAttribute("teacherInSession", teacher);
                this.forward(request, response, "/store?command=storeInfo");
            } else {
                TeacherEntity teacher = teacherFacade.find(Integer.valueOf(idParam));
                this.parseRequest(request, teacher);
                teacherFacade.edit(teacher);
                request.getSession(true).setAttribute("teacherInSession", teacher);
                this.forward(request, response, "/menu?command=menuList");
            }
        } catch (Exception e) {
            error(request, response, e.getMessage());
        }
    }
    
    

    void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String teachername = request.getParameter("teachername");
        String password = request.getParameter("password");
        System.out.println("UserServlet.login() username = " + teachername + " password=" + password);
        //teacherFacade.
        TeacherEntity teacher = teacherFacade.findByName(teachername);
        if (teacher != null && teacher.getPassword().equals(password)) {
            request.getSession(true).setAttribute("teacherInSession", teacher);
            forward(request, response, "/student");
        } else {
            request.setAttribute("errMsg", "输入信息有误,无法登录.");
            forward(request, response, "/teacher/login.jsp");
        }
    }
}
